package com.example.restatr.Atcivity.Hospital;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.restatr.Atcivity.Main.MainActivity;
import com.example.restatr.Atcivity.Hospital.Bean.HospitalPatientBean;
import com.example.restatr.Atcivity.Hospital.Bean.HospitalReservationBean;
import com.example.restatr.R;
import com.example.restatr.Tools;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class HospitalPatientAt extends AppCompatActivity {
    private Toolbar toolbar;
    private TextView barTitle;
    private EditText search;
    private LinearLayout Null;
    private FloatingActionButton fab;
    private ListView lv;
    private List<HospitalPatientBean.RowsBean> rowsBeans = new ArrayList<>();
    private List<HospitalReservationBean.RowsBean> reservationBean = new ArrayList<>();
    private TextView nullText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_patient_at);

        toolbar = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        Null = findViewById(R.id.Null);
        fab = findViewById(R.id.fab);
        lv = findViewById(R.id.lv);
        nullText = findViewById(R.id.null_text);

        Tools.SetBarTitle(toolbar, barTitle, getIntent().getStringExtra("intent"), search, this,false);

        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HospitalPatientAt.this, HospitalSetPatientAt.class);
                if (getIntent().getStringExtra("intent").equals("预约")){
                    intent.putExtra("intent", "预约");
                }else {
                    intent.putExtra("intent", "添加信息");
                }
                startActivity(intent);
            }
        });
    }
    //获取就诊列表
    private void GetPatient() {
        rowsBeans.clear();
        new Thread(() -> {
            String get = Tools.Get("/prod-api/api/hospital/patient/list", MainActivity.sp.getString("token", ""));
            HospitalPatientBean hospitalPatientBean = new Gson().fromJson(get, HospitalPatientBean.class);
            rowsBeans.addAll(hospitalPatientBean.getRows());
            runOnUiThread(() -> {
                if (rowsBeans.size() > 0) {
                    lv.setAdapter(new lvAdapter());
                } else {
                    Null.setVisibility(View.VISIBLE);
                }
            });
        }).start();
    }
    //获取预约列表
    private void GetReservation() {
        reservationBean.clear();
        new Thread(() -> {
            String token = Tools.Get("/prod-api/api/hospital/reservation/list", MainActivity.sp.getString("token", ""));
            HospitalReservationBean hospitalReservationBean = new Gson().fromJson(token, HospitalReservationBean.class);
            reservationBean.addAll(hospitalReservationBean.getRows());
            runOnUiThread(() -> {
                if (reservationBean.size() > 0) {
                    lv.setAdapter(new lvAdapter());
                } else {
                    Null.setVisibility(View.VISIBLE);
                    nullText.setText("暂无预约...");
                }
            });
        }).start();
    }

    private String GetType(String type) {
        if (type.equals("1")) {
            return "专家诊";
        } else {
            return "普通诊";
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (getIntent().getStringExtra("intent").equals("预约")) {
            GetReservation();
        } else if (getIntent().getStringExtra("intent").equals("就诊")) {
            GetPatient();
        }
    }

    class lvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            if (getIntent().getStringExtra("intent").equals("预约")) {
                return reservationBean.size();
            } else {
                return rowsBeans.size();
            }
        }

        @Override
        public Object getItem(int position) {
            if (getIntent().getStringExtra("intent").equals("预约")) {
                return reservationBean.get(position);
            } else {
                return rowsBeans.get(position);
            }
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.hospital_patient_item, parent, false);
            RelativeLayout card = convertView.findViewById(R.id.card);
            TextView licenseNo = convertView.findViewById(R.id.licenseNo);
            TextView licenseLevel = convertView.findViewById(R.id.licenseLevel);
            TextView score = convertView.findViewById(R.id.score);
            TextView idCard = convertView.findViewById(R.id.idCard);
            TextView auditOffice = convertView.findViewById(R.id.auditOffice);
            TextView applyDate = convertView.findViewById(R.id.applyDate);

            if (getIntent().getStringExtra("intent").equals("预约")) {
                licenseNo.setText(reservationBean.get(position).getPatientName());
                licenseLevel.setText("门诊类型：" +GetType(reservationBean.get(position).getType()));
                score.setText("科室名称：" + reservationBean.get(position).getCategoryName());
                idCard.setText("预约单号：" + reservationBean.get(position).getOrderNo());
                auditOffice.setText("金额：" + reservationBean.get(position).getMoney());
                applyDate.setText("预约时间：" + reservationBean.get(position).getReserveTime());
            } else {
                licenseNo.setText(rowsBeans.get(position).getName());
                licenseLevel.setText("性别：" + Tools.SetSex(rowsBeans.get(position).getSex()));
                score.setText("手机号：" + rowsBeans.get(position).getTel());
                idCard.setText("身份证：" + rowsBeans.get(position).getCardId());
                auditOffice.setText("地址：" + rowsBeans.get(position).getAddress());
                applyDate.setText("出生年月：" + rowsBeans.get(position).getBirthday());
                card.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(HospitalPatientAt.this, HospitalSetPatientAt.class);
                        intent.putExtra("data", new Gson().toJson(rowsBeans.get(position)));
                        intent.putExtra("intent", "修改信息");
                        startActivity(intent);
                    }
                });
            }


            return convertView;
        }
    }
}